package com.cn.kehong.service;

import com.cn.kehong.api.IPowRateDetailService;
import com.cn.kehong.api.IPowRateService;
import com.cn.kehong.dao.PowRateMapper;
import com.cn.kehong.domain.PowRate;
import com.cn.kehong.domain.PowRateDetail;
import com.cn.kehong.domain.PowRateExample;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PowRateServiceImpl implements IPowRateService {

    @Autowired
    private PowRateMapper powRateMapper;

    @Autowired
    private IPowRateDetailService powRateDetailService;

    @Override
    public List<PowRate> getPowRateList(QueryForm queryForm) throws Exception {
        int count = powRateMapper.selectPowRateListCount(queryForm);
        if (count == 0) {
            return Lists.newArrayList();
        }
        List<PowRate> powRates = powRateMapper.selectPowRateList(queryForm);
        for(PowRate rate :powRates){
            List<PowRateDetail> details = rate.getPowRateDetails();
            if(CollectionUtils.isNotEmpty(details)){
                String gradeStove = "";
                for (PowRateDetail detail:details){
                    String gradeValue = detail.getGradeVaule();
                    String stove = detail.getStoveNum();
                    if(StringUtils.isNotEmpty(gradeStove)){
                        gradeStove += "<br>";
                    }
                    gradeStove +=(gradeValue+":"+stove);
                }
                rate.setGradeStove(gradeStove);
            }
        }
        powRates.get(0).setCount(count);
        return powRates;
    }

    @Override
    public String addPowRate(PowRate powRate) throws Exception {
        String factory = powRate.getFactoryName();
        String powDate = powRate.getPowTime();
        if(StringUtils.isEmpty(factory)||StringUtils.isEmpty(powDate)){
            return "缺少必填参数！";
        }
        String id = CommonUtil.generateRandomNum("pow-");
        powRate.setId(id);
        List<PowRateDetail> powRateDetails = powRate.getPowRateDetails();
        if(CollectionUtils.isEmpty(powRateDetails)){
            return "参数异常！";
        }
        for(PowRateDetail detail:powRateDetails){
            detail.setPowId(id);
        }
        PowRateExample example = new PowRateExample();
        example.createCriteria().andFactoryNameEqualTo(factory).andPowTimeEqualTo(powDate).andStatusEqualTo(Constants.NORMAL_STATUS);
        int existCount = powRateMapper.countByExample(example);
        if(existCount > 0){
            return "产能利用率已经存在！";
        }
        int count = powRateMapper.insertSelective(powRate);
        if(count < 1){
            return "插入数据库异常！";
        }

        String resp = powRateDetailService.addPowRateDetail(powRateDetails);

        if(StringUtils.isNotEmpty(resp)){
            throw new Exception(resp);
        }
        return resp;
    }

    @Override
    public String updatePowRate(PowRate powRate) throws Exception {
        String id = powRate.getId();
        if(StringUtils.isEmpty(id)){
            return "缺少主键Id";
        }
        List<PowRateDetail> powRateDetails = powRate.getPowRateDetails();
        if(CollectionUtils.isEmpty(powRateDetails)){
            return "参数异常！";
        }
        for(PowRateDetail detail:powRateDetails){
            detail.setPowId(powRate.getId());
        }
        PowRateExample example = new PowRateExample();
        example.createCriteria().andIdEqualTo(id);
        int count = powRateMapper.updateByExampleSelective(powRate,example);
        if(count < 1){
            return "更新数据库异常！";
        }
        String resp = powRateDetailService.updatePowRateDetail(powRateDetails);

        if(StringUtils.isNotEmpty(resp)){
            throw new Exception(resp);
        }
        return resp;
    }

    @Override
    public String deletePowRateById(String id) throws Exception {
        if(StringUtils.isEmpty(id)){
            return "缺少主键Id";
        }
        PowRateExample example = new PowRateExample();
        example.createCriteria().andIdEqualTo(id);
        PowRate update =  new PowRate();
        update.setStatus(Constants.DEL_STATUS);
        int count = powRateMapper.updateByExampleSelective(update,example);
        return CommonUtil.outStr(count);
    }
}
